package com.woniuxy.userassembly.controller;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import com.woniuxy.api.PersonApi;
import com.woniu.cloud.common.dto.Result;
import com.woniuxy.cloud.UserAPI;
import com.woniuxy.cloud.entity.TUser;
import com.woniuxy.cloud.form.*;
import com.woniuxy.form.ServiceSettlementShoppingCart;
import com.woniuxy.form.SettlementShoppingCart;
import io.seata.spring.annotation.GlobalTransactional;
import io.swagger.annotations.*;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;

@RestController
@Api(tags = "用户个人管理", description = "提供用户相关的 API")
@GlobalTransactional
public class PersonApiImp implements PersonApi {


    @Resource
    UserAPI userAPI;
    /**
     * 注册 ，调用生成注册验证码的接口\n" +
     *             "  用户注册接口\n" +
     *             "验证电话是否已经注册的接口\n" +
     *             "新增优惠劵
     * @param registerForm
     * @return
     */
    @PostMapping("/permit/registry")
    @ApiOperation("注册")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "phone", value = "电话", dataType = "string"),
            @ApiImplicitParam(name = "username", value = "用户名", dataType = "string"),
            @ApiImplicitParam(name = "password", value = "用户密码", dataType = "string")
    })
    @ApiResponses({
            @ApiResponse(code = 200, message = "OK", response = Boolean.class)
    })
    @Override
    @GlobalTransactional
    public Result registry(RegisterForm registerForm) {
        Result register = userAPI.register(registerForm);
        TUser tUser = JSONObject.parseObject(JSON.toJSONString(register.getData()), TUser.class);
        AddCouponForm addCouponForm = new AddCouponForm();
        addCouponForm.setNumber(2);
        addCouponForm.setCouponId(1);
        addCouponForm.setUserId(tUser.getUserId());
        userAPI.insetCouponByUserId(addCouponForm);
        return register;
    }

    /**
     * 登录 ，调用生成注册验证码的接口\n" +
     *             "  密码登录
     * @param userName 用户名
     * @param userPhone 手机号
     * @param passWord 密码
     * @return
     */
    @GetMapping("/permit/loginByPassword")
    @ApiOperation("密码登录")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "userName", value = "用户名", dataType = "string"),
            @ApiImplicitParam(name = "userPhone", value = "手机号", dataType = "string"),
            @ApiImplicitParam(name = "password", value = "用户密码", dataType = "string"),
    })
    @ApiResponses({
            @ApiResponse(code = 200, message = "OK", response = Boolean.class)
    })
    @Override
    @GlobalTransactional
    public Result loginByPassword(String userName, String userPhone, String passWord) {
        Result login = userAPI.login(userName, userPhone, passWord);
        return login;
    }

    /**
     * 登录 ，调用生成注册验证码的接口\n" +
     *             "  验证码登录
     * @param code
     * @param userPhone
     * @param userName
     * @return
     */
    @GetMapping("/permit/loginByVerifyCode")
    @ApiOperation("验证码登录")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "code", value = "验证码", dataType = "string"),
            @ApiImplicitParam(name = "userPhone", value = "手机号", dataType = "string"),
            @ApiImplicitParam(name = "userName", value = "用户名", dataType = "string"),
    })
    @ApiResponses({
            @ApiResponse(code = 200, message = "OK", response = Boolean.class)
    })
    @Override
    @GlobalTransactional
    public Result loginByVerifyCode(String code, String userPhone, String userName) {
        Result result = userAPI.loginByUserPhone(userPhone, code);
        return result;
    }


    @GetMapping("/permit/getLoginMessage")
    @ApiOperation("生成登录验证码的接口")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "userPhone", value = "手机号", dataType = "string"),
            @ApiImplicitParam(name = "userName", value = "用户名", dataType = "string")
    })
    @ApiResponses({@ApiResponse(code = 200, message = "OK", response = Boolean.class)})
    @Override
    @GlobalTransactional
    public Result getLoginMessage(@RequestParam("userName") String userName, @RequestParam("userPhone") String userPhone) {
        Result loginMessage = userAPI.getLoginMessage(userName, userPhone);
        return loginMessage;
    }


    @GetMapping("/permit/getUpdateMessage")
    @ApiOperation("获取修改密码验证码")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "userPhone", value = "手机号", dataType = "string"),
            @ApiImplicitParam(name = "userName", value = "用户名", dataType = "string")
    })
    @ApiResponses({@ApiResponse(code = 200, message = "OK", response = Boolean.class)})
    @Override
    @GlobalTransactional
    public Result getUpdateMessage(String userName, String userPhone) {
        return null;
    }

    @GetMapping("/permit/getRegisterMessageByUserPhone")
    @ApiOperation("获取注册验证码")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "userPhone", value = "手机号", dataType = "string")
    })
    @ApiResponses({@ApiResponse(code = 200, message = "OK", response = Boolean.class)})
    @Override
    @GlobalTransactional
    public Result getRegisterMessageByUserPhone(String userPhone) {
        return null;
    }


    @GetMapping("/permit/updatePassword")
    @ApiOperation("调用生成验证码接口\n" +
            "修改密码接口")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "userName", value = "手机号", dataType = "string"),
            @ApiImplicitParam(name = "userPhone", value = "用户名", dataType = "string"),
            @ApiImplicitParam(name = "number", value = "验证码", dataType = "string"),
            @ApiImplicitParam(name = "newPassWord", value = "新密码", dataType = "string")
    })
    @ApiResponses({@ApiResponse(code = 200, message = "OK", response = Boolean.class)})
    @Override
    @GlobalTransactional
    public Result updatePassword(String userName, String userPhone, String number, String newPassWord) {
        Result result = userAPI.updatePassWord(number, newPassWord);
        return result;
    }


    @PutMapping("/updateUserProfilePhotoByUserId")
    @ApiOperation("修改用户头像接口")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "file", value = "头像文件", dataType = "MultipartFile"),
    })
    @ApiResponses({@ApiResponse(code = 200, message = "OK", response = Boolean.class)})
    @Override
    @GlobalTransactional
    public Result updateUserProfilePhotoByUserId(MultipartFile file) {
//        HttpServletRequest request = ((ServletRequestAttributes) RequestContextHolder.getRequestAttributes()).getRequest();
//        String userId1 = request.getHeader("userId");
//        Integer userId = Integer.parseInt(userId1);
        Integer userId = 1;
        Result result = null;
        try {
            result = userAPI.updateUserProfilePhotoByUserId(userId,file);
        } catch (IOException e) {
            e.printStackTrace();
        }
        return result;
    }

    @PutMapping("/updatePersonPayCode")
    @ApiOperation("更新用户支付密码")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "code", value = "验证码", dataType = "String"),
            @ApiImplicitParam(name = "phoneNum", value = "手机号", dataType = "String"),
            @ApiImplicitParam(name = "newPayCode", value = "新的支付密码", dataType = "String"),
    })
    @ApiResponses({@ApiResponse(code = 200, message = "OK", response = Boolean.class)})
    @Override
    @GlobalTransactional
    public Result updatePersonPayCode(String userPaymentPassWord, String newPassWord) {
//        HttpServletRequest request = ((ServletRequestAttributes) RequestContextHolder.getRequestAttributes()).getRequest();
//        String userId1 = request.getHeader("userId");
//        Integer userId = Integer.parseInt(userId1);
        Integer userId = 1;
        Result result = userAPI.updateUserPassWordByUserId(userId,userPaymentPassWord, newPassWord);
        return result;
    }


    @PutMapping("/updateDefaultAddress")
    @ApiOperation("修改用户默认地址")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "userAddressId", value = "新的默认用户地址id", dataType = "Integer"),
    })
    @ApiResponses({@ApiResponse(code = 200, message = "OK", response = Boolean.class)})
    @Override
    @GlobalTransactional
    public Result updateDefaultAddress(Integer userAddressId) {
        Result result = userAPI.updateDefaultAddress(userAddressId);
        return result;
    }

    @PutMapping("/updateUserNickNameByUserId")
    @ApiOperation("修改用户昵称")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "response", value = "响应", dataType = "HttpServletResponse")
    })
    @ApiResponses({@ApiResponse(code = 200, message = "OK", response = Boolean.class)})
    @Override
    @GlobalTransactional
    public Result updateUserNickNameByUserId(String newName) {
        //        HttpServletRequest request = ((ServletRequestAttributes) RequestContextHolder.getRequestAttributes()).getRequest();
//        String userId1 = request.getHeader("userId");
//        Integer userId = Integer.parseInt(userId1);
        Integer userId = 1;
        Result result = userAPI.updateUserNickNameByUserId(userId,newName);
        return result;
    }

    @PutMapping("/insetCar")
    @ApiOperation("新增用户车型")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "userCarCarSeriesId", value = "用户车id", dataType = "Integer"),
    })
    @ApiResponses({@ApiResponse(code = 200, message = "OK", response = Boolean.class)})
    @Override
    @GlobalTransactional
    public Result insetCar(Integer userCarCarSeriesId) {
        //        HttpServletRequest request = ((ServletRequestAttributes) RequestContextHolder.getRequestAttributes()).getRequest();
//        String userId1 = request.getHeader("userId");
//        Integer userId = Integer.parseInt(userId1);
        Integer userId = 1;
        Result result = userAPI.insertCar(userId,userCarCarSeriesId);
        return result;
    }

    @PutMapping("/updateDefaultCarByUserCarId")
    @ApiOperation("修改用户默认车辆")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "userCarId", value = "用户车辆id", dataType = "Integer"),
    })
    @ApiResponses({@ApiResponse(code = 200, message = "OK", response = Boolean.class)})
    @Override
    @GlobalTransactional
    public Result updateDefaultCarByUserCarId(Integer userCarId) {
        //        HttpServletRequest request = ((ServletRequestAttributes) RequestContextHolder.getRequestAttributes()).getRequest();
//        String userId1 = request.getHeader("userId");
//        Integer userId = Integer.parseInt(userId1);
        Integer userId = 1;
        Result result = userAPI.updateDefaultCarByUserCarId(userId,userCarId);
        return result;
    }

    @PutMapping("/deleteCarByUserCarId")
    @ApiOperation("用户移除爱车")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "userCarId", value = "用户车型id", dataType = "HttpServletResponse")
    })
    @ApiResponses({@ApiResponse(code = 200, message = "OK", response = Boolean.class)})
    @Override
    @GlobalTransactional
    public Result deleteCarByUserCarId(Integer userCarId) {
        Result result = userAPI.deleteCarByUserCarId(userCarId);
        return result;
    }

    @PutMapping("/updateAddressByUserId")
    @ApiOperation("修改用户地址")
    @ApiResponses({@ApiResponse(code = 200, message = "OK", response = Boolean.class)})
    @Override
    @GlobalTransactional
    public Result updateAddressByUserId(UpdateUserAddressForm updateUserAddressForm) {
        Result result = userAPI.updateAddressByUserAddressId(updateUserAddressForm);
        return result;
    }


    @PutMapping("/insertAddressByUserId")
    @ApiOperation("添加收货地址")
    @ApiResponses({@ApiResponse(code = 200, message = "OK", response = Boolean.class)})
    @Override
    @GlobalTransactional
    public Result insertAddressByUserId(UserAddressForm userAddressForm) {
        Result result = userAPI.insertAddress(userAddressForm);
        return result;
    }




}
